﻿using System;

using System.Collections.Generic;

using JSON;

namespace JSONTest
{
    public class Results : IJSONObject
    {
        private int m_Offset;     
        private List<SearchResult> m_Results;
        private int m_Total;


        private Results() { m_Results = new List<SearchResult>(); }

        public static Results Search(string q)
        {
            com.msn.search.soap.MSNSearchService searchService = new com.msn.search.soap.MSNSearchService();

            com.msn.search.soap.SearchRequest sRequest = new com.msn.search.soap.SearchRequest();
            sRequest.CultureInfo = "en-us";
            sRequest.Query = q;
            sRequest.AppID = "4D8FA862CA634A6A1538ADC99930E82FC44E8FEB";

            sRequest.Requests = new com.msn.search.soap.SourceRequest[1];
            com.msn.search.soap.SourceRequest sReq = new com.msn.search.soap.SourceRequest();
            sReq.Source = com.msn.search.soap.SourceType.Web;
            sReq.Count=10;
            sReq.Offset=0;

            sRequest.Requests[0] = sReq;

            com.msn.search.soap.SearchResponse sResponse = searchService.Search(sRequest);


            Results lResult = new Results();
            if (sResponse.Responses.Length > 0)
            {
                
                lResult.m_Offset = sResponse.Responses[0].Offset;
                lResult.m_Total = sResponse.Responses[0].Total;

                foreach (com.msn.search.soap.Result sR in sResponse.Responses[0].Results)
                {
                    SearchResult mySr = new SearchResult();

                 
                       mySr.Description = sR.Description;
                    

                    mySr.Title = sR.Title;
                    mySr.URL = sR.Url;

                    lResult.m_Results.Add(mySr);
                }
            }
            return lResult;
        }


        #region IJSONObject Members

        public JSONCollection WriteObject()
        {   
            JSONNumber<int> jTotal = new JSONNumber<int>("Total", m_Total);
            JSONNumber<int> jOffset = new JSONNumber<int>("Offset", m_Offset);

            List<JSONCollection> ResultObjects = new List<JSONCollection>(m_Results.Count);
            foreach (SearchResult r in m_Results)
            {
                ResultObjects.Add(r.WriteObject());
            }

            JSONArray jResults = new JSONArray("Result_List", ResultObjects);

            JSONObject js = new JSONObject("LiveResults", jTotal, jOffset, jResults);
            return js;
        }

        #endregion

        
    }

    public class SearchResult : IJSONObject
    {
        
        private string m_Description;

        public string Description
        {
            get { return m_Description; }
            set { m_Description = value; }
        }
       
        private string m_Title;

        public string Title
        {
            get { return m_Title; }
            set { m_Title = value; }
        }
        private string m_URL;

        public string URL
        {
            get { return m_URL; }
            set { m_URL = value; }
        }

        #region IJSONObject Members

        public JSONCollection WriteObject()
        {    
            JSONString jDescription = new JSONString("Description", m_Description);
            JSONString jTitle = new JSONString("Title", m_Title);
            JSONString jURL= new JSONString("URL", m_URL);


            JSONObject js = new JSONObject("Result", jDescription, jTitle, jURL);
            return js;
        }

        #endregion
    }

   
}